package com.example.health_life.service;

import com.example.health_life.dao.Course;
import com.example.health_life.dao.Video;
import com.example.health_life.dao.VideoRepository;
import com.example.health_life.dto.UsersDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class VideoServiceImpl implements VideoService {
    
    @Autowired
    private VideoRepository videoRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private CourseService courseService;

    @Override
    public List<Video> getVideoList() {
        return videoRepository.findAll();
    }

    @Override
    public List<Video> getVideosByCourseId(Integer courseId) {
        // 这里需要在VideoRepository中添加findByCourseId方法
        return videoRepository.findByCourseId(courseId);
    }

    @Override
    public Video getVideoById(Integer id) {
        return videoRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("视频不存在"));
    }

    @Override
    public int addVideo(Video video) {
        // 设置默认值
        if (video.getOrderNum() == null) {
            video.setOrderNum(0);
        }
        // 设置创建时间
        video.setCreateTime(java.time.Instant.now());
        // 设置状态为正常
        video.setStatus((byte) 1);
        return videoRepository.save(video).getId();
    }

    @Override
    public int deleteVideo(Integer id) {
        // 判断视频是否存在
        Video video = videoRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("视频不存在"));

        // 存在，则将其status设置相反数
        byte status = (byte) (video.getStatus() == 1 ? -1 : 1);
        video.setStatus(status);
        videoRepository.save(video);
        return id;
    }

    @Override
    public int updateVideo(Integer id, String title, String url, Integer duration, 
                          Integer orderNum, Byte status, String coverImg) {
        // 判断视频是否存在
        Video video = videoRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("视频不存在"));

        // 更新视频信息
        if (title != null) {
            video.setTitle(title);
        }
        if (url != null) {
            video.setUrl(url);
        }
        if (duration != null) {
            video.setDuration(duration);
        }
        if (orderNum != null) {
            video.setOrderNum(orderNum);
        }
        if (status != null) {
            video.setStatus(status);
        }
        if (coverImg != null) {
            video.setCoverImg(coverImg);
        }

        videoRepository.save(video);
        return id;
    }

    @Override
    public List<Video> getVideoListWithAuthor() {
        return getVideoListWithAuthorByStatus(1);
    }

    @Override
    public List<Video> getVideoListWithAuthorByStatus(Integer status) {
        List<Video> videos;
        if (status == 0) {
            videos = videoRepository.findAll();
        } else {
            videos = videoRepository.findByStatus((byte) status.intValue());
        }
        for (Video video : videos) {
            // 通过课程ID获取作者信息
            Course course = courseService.getCourseById(video.getCourseId());
            if (course != null) {
                UsersDTO author = userService.getUserById(course.getAuthorId());
                if (author != null) {
                    video.setAuthorNickname(author.getNickname());
                }
            }
        }
        return videos;
    }

    @Override
    public Video getVideoByIdWithAuthor(Integer id) {
        Video video = videoRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("视频不存在"));
        // 通过课程ID获取作者信息
        Course course = courseService.getCourseById(video.getCourseId());
        if (course != null) {
            UsersDTO author = userService.getUserById(course.getAuthorId());
            if (author != null) {
                video.setAuthorNickname(author.getNickname());
            }
        }
        return video;
    }

    @Override
    public List<Video> getVideosByCourseIdWithAuthor(Integer courseId) {
        List<Video> videos = videoRepository.findByCourseId(courseId);
        // 获取课程信息
        Course course = courseService.getCourseById(courseId);
        if (course != null) {
            UsersDTO author = userService.getUserById(course.getAuthorId());
            if (author != null) {
                // 为所有视频设置相同的作者昵称
                for (Video video : videos) {
                    video.setAuthorNickname(author.getNickname());
                }
            }
        }
        return videos;
    }
}
